package com.didispace.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


@RestController
public class HelloController {


	@Autowired
	private DiscoveryClient client;

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private HelloService02 helloService02;

	@RequestMapping("/hello")
	public String hello(){
		ServiceInstance instance = client.getLocalServiceInstance();
		System.out.println("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
		return "Hello World";
	}

	/**
	 * 通过RestTemplate方式结合url和服务名调用
	 * @return
	 */
	@RequestMapping("/getH2")
	public String getH2(){
		return restTemplate.getForEntity("http://hello-service-02/helloTwo", String.class).getBody();
	}

	/**
	 * 通过Feign直接调用方法
	 * @return
	 */
	@RequestMapping("/getH2ByFeign")
	public String getH2ByFeign() {
		return helloService02.helloTwo();
	}
}